package com.example.yichuangyun.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.GridLayoutManager;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.DialogCallBinding;
import com.example.yichuangyun.model.business.NumberModel;
import com.example.yichuangyun.ui.adapter.EmptyRecyclerCommonAdapter;
import com.example.yichuangyun.ui.adapter.RecyclerViewHolder;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.ToastHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 拨打电话弹窗
 */
public class CallDialog {
    private Dialog dialog;
    private DialogCallBinding binding;
    private final StringBuilder builder = new StringBuilder();

    public CallDialog(Activity activity) {
        if (dialog == null) {
            getDialog(activity);
        }
    }

    private void getDialog(Activity activity) {
        binding = DialogCallBinding.inflate(LayoutInflater.from(activity));
        dialog = new Dialog(activity, R.style.prompt_progress_dialog_dimenabled);
        dialog.setContentView(binding.getRoot(), new LinearLayout.LayoutParams
                (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        dialog.setCancelable(true);
        Window dialogWindow = dialog.getWindow();
        // 去除遮罩效果
        dialogWindow.setDimAmount(0f);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setWindowAnimations(R.style.main_menu_animStyle);
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialogWindow.setAttributes(lp);
        binding.recyclerView.setLayoutManager(new GridLayoutManager(activity, 3));
        List<NumberModel> numData = new ArrayList<>();
        numData.add(new NumberModel(1, ""));
        numData.add(new NumberModel(2, "ABC"));
        numData.add(new NumberModel(3, "DEF"));
        numData.add(new NumberModel(4, "GHI"));
        numData.add(new NumberModel(5, "JKL"));
        numData.add(new NumberModel(6, "MNO"));
        numData.add(new NumberModel(7, "PQRS"));
        numData.add(new NumberModel(8, "TUV"));
        numData.add(new NumberModel(9, "WXYZ"));
        EmptyRecyclerCommonAdapter<NumberModel> adapter = new EmptyRecyclerCommonAdapter<NumberModel>(activity, R.layout.item_number, numData) {
            @Override
            public void convert(RecyclerViewHolder holder, NumberModel item, int position) {
                holder.setText(R.id.tv_num, String.valueOf(item.getNum()));
                holder.setText(R.id.tv_des, CommonUtil.checkText(item.getDes()));
                holder.setViewVisibility(R.id.tv_des, TextUtils.isEmpty(item.getDes()) ? View.INVISIBLE : View.VISIBLE);
            }
        };
        binding.recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener((parent, view, position) -> setNum(String.valueOf(numData.get(position).getNum())));

        binding.tvXing.setOnClickListener(v -> {
            setNum(binding.tvXing.getText().toString());
        });

        binding.tvZero.setOnClickListener(v -> {
            setNum(binding.tvZero.getText().toString());
        });

        binding.tvJing.setOnClickListener(v -> {
            setNum(binding.tvJing.getText().toString());
        });

        binding.ivCancel.setOnClickListener(v -> dialog.dismiss());

        binding.ivCall.setOnClickListener(v -> callNum(builder.toString()));

        binding.ivDelete.setOnClickListener(v -> {
            if (builder.length() > 0) {
                builder.deleteCharAt(builder.length() - 1);
                clickNum(builder.toString());
            }
        });
    }

    private void setNum(String num) {
        if (builder.toString().length() < 11) {
            builder.append(num);
            clickNum(builder.toString());
        } else {
            ToastHelper.show("请输入正确的电话号码");
        }
    }

    public void clickNum(String num) {

    }

    public void callNum(String num) {

    }

    public void showDialog() {
        dialog.show();
    }

    public void dismissDialog() {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }
}
